import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../common/api/user_api.dart';
import '../../common/const/app_color_const.dart';
import '../../common/const/app_style_const.dart';
import '../../common/type/user_type.dart';
import '../../common/util/common_util.dart';
import '../../common/util/store_util.dart';
import '../../router_table.dart';

class LaunchScreenPage extends StatefulWidget {
  const LaunchScreenPage({Key? key}) : super(key: key);

  @override
  State<LaunchScreenPage> createState() => LaunchScreenPageState();
}

class LaunchScreenPageState extends State<LaunchScreenPage>
    with SingleTickerProviderStateMixin {
  Duration keepTimer = const Duration(seconds: 1);

  int get showSecond => keepTimer.inSeconds;

  late Timer timer;

  toHome() {
    StoreUtil.getAccount().then((value) {
      if (null != value && null != value.employeeNum && null != value.pwd) {
        UserApi.login({"employeeNum": value.employeeNum, "pwd": value.pwd})
            .then((res) {
          if (null != res.data && res.data["isSuccess"]) {
            var data = res.data["data"];
            StoreUtil.setUserId(data["id"]);
            StoreUtil.setToken(data["token"]);
            StoreUtil.setUserInfo(UserInfoType.fromJson(data));
            CommonUtil.showToast("登录成功！");
            Navigator.of(context)
                .pushNamedAndRemoveUntil(RouterTable.homePath, (route) => false);
          } else {
            CommonUtil.showToast(res.data["msg"]);
          }
        });
      } else {
        Navigator.of(context)
            .pushNamedAndRemoveUntil(RouterTable.loginPath, (route) => false);
      }
    });
  }

  @override
  void initState() {
    super.initState();

    Duration step = const Duration(seconds: 1);
    timer = Timer.periodic(const Duration(seconds: 1), (_) {
      setState(() {
        keepTimer -= step;
      });
      if (keepTimer == Duration.zero) {
        timer.cancel();
        toHome();
      }
    });
  }

  @override
  void dispose() {
    timer.cancel();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
        overlays: SystemUiOverlay.values);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
        overlays: [SystemUiOverlay.top]);
    return Scaffold(
      backgroundColor: launchScreenBg,
      body: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          Positioned(
            right: 10,
            bottom: 10,
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextButton(
                onPressed: toHome,
                child: Text('跳过${showSecond > 0 ? showSecond : ''}'),
              ),
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: 110.0,
                height: 110.0,
                padding: const EdgeInsets.all(16.0),
                decoration: const BoxDecoration(
                    color: loginLogoBg,
                    borderRadius: BorderRadius.all(Radius.circular(60))),
                child: Image.asset(
                  'assets/images/app_icon_1x.png',
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 10.0),
                child: Text(
                  CommonUtil.getEnvConfig()["appName"],
                  style: launchScreenTitleStyle,
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}
